unit uMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs, ScktComp, ExtCtrls, strutils;

type
  TLivePokerRefreshService = class(TService)
    ServerSocket1: TServerSocket;
    tmr_CheckActiveClients: TTimer;
    procedure tmr_CheckActiveClientsTimer(Sender: TObject);
    procedure ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure ServerSocket1ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure ServiceCreate(Sender: TObject);
  private
    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

var
  LivePokerRefreshService: TLivePokerRefreshService;

implementation

{$R *.DFM}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  LivePokerRefreshService.Controller(CtrlCode);
end;

function TLivePokerRefreshService.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TLivePokerRefreshService.ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
var
  i: Integer;
begin

end;

procedure TLivePokerRefreshService.ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
var
  i: Integer;
begin
  tmr_CheckActiveClients.Enabled := True;
end;

procedure TLivePokerRefreshService.ServerSocket1ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
  ErrorCode := 0; // this line solve your problems about asyncroneus socket error
end;

procedure TLivePokerRefreshService.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
  i: Integer;
  sRec: string;
begin
  sRec := Socket.ReceiveText;
  if sRec <> '' then
  begin
    for i := 0 to ServerSocket1.Socket.ActiveConnections - 1 do
    begin
      if Socket.Handle <> ServerSocket1.Socket.Connections[i].Handle then
      begin
        // EventType + '|1|' + TournamentID + '|2|' + TableID
        ServerSocket1.Socket.Connections[i].SendText(sRec);
      end;
    end;
  end;
end;

procedure TLivePokerRefreshService.ServiceCreate(Sender: TObject);
begin
  if LeftStr(ParamStr(1), 6) = '-port:' then
    ServerSocket1.Port := StrToInt(MidStr(ParamStr(1), 7, Length(ParamStr(1))))
  else
    ServerSocket1.Port := 7781;
  ServerSocket1.Active := True;
end;

procedure TLivePokerRefreshService.tmr_CheckActiveClientsTimer(Sender: TObject);
var
  i: Integer;
begin
  tmr_CheckActiveClients.Enabled := false;
end;

end.
